// import { LgetItem, LsetItem } from '@/utils/storage'
import api from '@/api'
const configs = {
  namespaced: true,
  state: {
    tabbar: [],
    basic: {},
    ad: {},
    page: {},
    notice: {},
    rule: {},
    signup: {},
    wechat: {},
    security: {},
    commentary: {},
    share:{}
  },
  getters: {
    tabbar: state => state.tabbar,
    basic: state => state.basic,
    ad: state => state.ad,
    page: state => state.page,
    notice: state => state.notice,
    rule: state => state.rule,
    signup: state => state.signup,
    wechat: state => state.wechat,
    security: state => state.security,
    commentary: state => state.commentary,
    share: state => state.share
  },
  mutations: {
    SET_TABBAR: (state, list) => {
      state.tabbar = list
    },
    SET_BASIC: (state, basic) => {
      state.basic = basic
    },
    SET_AD: (state, ad) => {
      state.ad = ad
    },
    SET_PAGE: (state, page) => {
      state.page = page
    },
    SET_NOTICE: (state, notice) => {
      state.notice = notice
    },
    SET_RULE: (state, rule) => {
      state.rule = rule
    },
    SET_SIGN_UP: (state, signup) => {
      state.signup = signup
    },
    SET_WECHAT: (state, wechat) => {
      state.wechat = wechat
    },
    SET_SECURITY: (state, security) => {
      state.security = security
    },
    SET_COMMENTARY: (state, commentary) => {
      state.commentary = commentary
    },
    SET_SHARE: (state, share) => {
      state.share = share
    },



  },

  actions: {
    setNav({
      commit
    }, list) {
      //对tabbar进行排序
      return new Promise((resolve) => {
        list.sort((a, b) => {
          return b.position - a.position
        })
        let arr1 = list.filter(item => item.home && item.enabled)
        let arr2 = list.filter(item => !item.home && item.enabled)
        let navs = [].concat(arr1, arr2)
        navs.forEach(item => {
          switch (item.name) {
            case "投票":
              (item.icon = "iconrenyuan"), (item.link = "/vote");
              break;
            case "搜索":
              (item.icon = "iconsousuo"), (item.link = "/search");
              break;
            case "介绍":
              (item.icon = "iconxianchangziliao"), (item.link = "/introduce");
              break;
            case "排名":
              (item.icon = "iconqiandaotongji"), (item.link = "/ranking");
              break;
            case "报名":
              (item.icon = "iconzcpt-peixunbaomingguanli"), (item.link = "/sign");
              break;
            case "动态":
              (item.icon = "icondongtai_"), (item.link = "/dynamic");
              break;
            default:
              break;
          }
        })

        commit('SET_TABBAR', navs)

        resolve(navs)
      })
    }
  }
}

export const filterAsyncRouter = (routers) => {
  var arr = []
  routers.forEach(item => {
    if (item.enabled) {
      switch (item.name) {
        case "投票":
          arr.push({
            path: '/vote',
            component: loadView('vote'),
            meta: {
              title: item.name,
              icon: "iconrenyuan",
              navIndex: 1
            }
          })
          break;
        case "介绍":
          arr.push({
            path: '/introduce',
            component: loadView('introduce'),
            meta: {
              title: item.name,
              icon: "iconxianchangziliao",
              navIndex: 1
            }
          })
          break;
        case "排名":
          arr.push({
            path: '/ranking',
            component: loadView('ranking'),
            meta: {
              title: item.name,
              icon: "iconqiandaotongji",
              navIndex: 1
            }
          })
          break;
        case "报名":
          arr.push({
            path: '/sign',
            component: loadView('sign'),
            meta: {
              title: item.name,
              icon: "iconzcpt-peixunbaomingguanli",
              navIndex: 1
            }
          })
          break;
        case "动态":
          arr.push({
            path: '/dynamic',
            component: loadView('dynamic'),
            meta: {
              title: item.name,
              icon: "icondongtai_",
              navIndex: 1
            }
          })
          break;
        default:
          break;
      }
    }

  })
  return arr
}
export const loadView = (view) => {
  return () => import(`@/views/${view}/index.vue`)
}


export default configs